package com.diamond.service.impl;

import java.util.List;

import com.diamond.bo.OrderBO;
import com.diamond.bo.QueryOrderBO;
import com.diamond.bo.ReturnBO;
import com.diamond.bo.SuppOrderBO;
import com.diamond.bo.SuppOrderDetailBO;
import com.diamond.po.SuppOrderPO;
import com.diamond.service.ISupporderService;

/**
 * @author JSmart Tools
 */
@SuppressWarnings("unchecked") 
public class SupporderService extends Service<SuppOrderPO> implements ISupporderService {

	public ReturnBO<SuppOrderBO> querySuppOrder(QueryOrderBO queryOrderBo)
			throws Exception {
		ReturnBO<SuppOrderBO> returnBo = new ReturnBO<SuppOrderBO>();
		StringBuilder queryMSql = new StringBuilder("SELECT DISTINCT * FROM supporder so, supplier s, clientorder c, USER u WHERE so.supplier_id=s.supplier_id AND so.cliOrdMaster_id=c.cliOrdMaster_id AND c.user_id=u.user_id");
		StringBuilder totalSql = new StringBuilder("SELECT DISTINCT * FROM supporder so, supplier s, clientorder c, USER u WHERE so.supplier_id=s.supplier_id AND so.cliOrdMaster_id=c.cliOrdMaster_id AND c.user_id=u.user_id");
		if (!"".equals(queryOrderBo.getSupplierName()) && null != queryOrderBo.getSupplierName()) {
			queryMSql.append(" and s.supplier_name like '%").append(queryOrderBo.getSupplierName()).append("%'");
			totalSql.append(" and s.supplier_name like '%").append(queryOrderBo.getSupplierName()).append("%'");
		}
		if (!"".equals(queryOrderBo.getOrderDateFrom()) && null != queryOrderBo.getOrderDateFrom()
				&& !"".equals(queryOrderBo.getOrderDateTo()) && null != queryOrderBo.getOrderDateTo()) {
			queryMSql.append(" and (so.order_date BETWEEN '").append(queryOrderBo.getOrderDateFrom()).append("' and '").append(queryOrderBo.getOrderDateTo()).append("')");
			totalSql.append(" and (so.order_date BETWEEN '").append(queryOrderBo.getOrderDateFrom()).append("' and '").append(queryOrderBo.getOrderDateTo()).append("')");
		} 
		if (!"".equals(queryOrderBo.getOrderNo()) && null != queryOrderBo.getOrderNo()) {
			queryMSql.append(" and so.myorder_no like '").append(queryOrderBo.getOrderNo()).append("%'");
			totalSql.append(" and so.myorder_no like '").append(queryOrderBo.getOrderNo()).append("%'");
		}
		queryMSql.append(" order by so.order_date desc").append(" limit ").append(queryOrderBo.getStart()).append(", ").append(queryOrderBo.getPageSize());
		List<SuppOrderBO> list = super.genericDAO.findBySqlToObj(queryMSql.toString(), SuppOrderBO.class);
		List totallist = super.genericDAO.findBySqlToObj(totalSql.toString(), null);
		returnBo.setPageSize(queryOrderBo.getPageSize());
		returnBo.setStart(queryOrderBo.getStart());
		returnBo.setReturnBolist(list);
		returnBo.setType(1);
		returnBo.setTotalSize(totallist.size());
		return returnBo;
	}

	public OrderBO querySuppOrderById(String suppOrdMasterId) throws Exception {
		OrderBO orderBo = new OrderBO();
		String queryMasterSql = "SELECT DISTINCT * FROM supporder so, supplier s, clientorder c, USER u WHERE so.supplier_id=s.supplier_id AND so.cliOrdMaster_id=c.cliOrdMaster_id AND c.user_id=u.user_id and so.suppOrdMaster_id='" + suppOrdMasterId + "'";
		String queryDetailSql = "select * from supporderdetail sod where sod.suppOrderMaster_id='" + suppOrdMasterId + "'";
		List<SuppOrderBO> sordlist = super.genericDAO.findBySqlToObj(queryMasterSql, SuppOrderBO.class);
		List<SuppOrderDetailBO> sorddlist = super.genericDAO.findBySqlToObj(queryDetailSql, SuppOrderDetailBO.class);
		if (sordlist.size() > 0 && sordlist.size()==1 && sorddlist.size() > 0) {
			orderBo.setSuppOrderBo(sordlist.get(0));
			orderBo.setSuppOrderDetailBolist(sorddlist);
		}
		return orderBo;
	}
 

}
